<template>

  <!--
    	作者：luoyiming
    	时间：2019-10-25
    	描述：商品-添加
    -->
  <el-dialog title="添加消息" v-model="dialogVisible" @close='dialogFormVisible' :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form size="small" :model="form" ref="form">
      <el-form-item label="消息名稱" :label-width="formLabelWidth" :rules="[{required: true,message: ' '}]" prop="messageName">
        <el-input v-model="form.messageName" autocomplete="off" placeholder="請輸入消息名稱"></el-input>
      </el-form-item>
      <el-form-item label="名稱(英文唯一)" :label-width="formLabelWidth" :rules="[{required: true,message: ' '}]" prop="messageEname">
        <el-input v-model="form.messageEname" autocomplete="off" placeholder="請輸入消息英文名稱"></el-input>
      </el-form-item>
      <el-form-item label="通知對象" :label-width="formLabelWidth" :rules="[{required: true,message: ' '}]" prop="messageTo">
        <el-select v-model="form.messageTo" placeholder="請選擇通知對象">
          <el-option label="會員" :value="10"></el-option>
          <el-option label="商家" :value="20"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息類別" :label-width="formLabelWidth" :rules="[{required: true,message: ' '}]" prop="messageType">
        <el-select v-model="form.messageType" placeholder="請選擇消息類別">
          <el-option label="訂單" :value="10"></el-option>
          <el-option label="分銷" :value="20"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth">
        <el-input v-model="form.sort" placeholder="請輸入數字"></el-input>
      </el-form-item>
      <el-form-item label="備註" :label-width="formLabelWidth">
        <el-input v-model="form.remark" autocomplete="off" placeholder="請輸入備註"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
    <div  class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" @click="addClick()" :loading="loading">確 定</el-button>
    </div>
    </template>
  </el-dialog>
</template>

<script>
import MessageApi from '@/api/message.js';
export default {
  data() {
    return {
      form: {
        status: 0,
        sort: 100
      },
      categoryList: [],
      sort: '100',
      radio: '1',
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false
    };
  },
  props: ['open_add'],
  created() {
    this.dialogVisible = this.open_add;
  },
  methods: {
    /*添加插件*/
    addClick() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          MessageApi.addMessage(params, true).then(res => {
            if (res.code == 1) {
              self.loading = false;
              ElMessage({
                message: "恭喜你，添加成功",
                type: 'success'
              });
              self.dialogFormVisible(true);
            } else {
              self.loading = false;
            }
          }).catch(error => {});
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    }
  }
};
</script>

